<template>
  <div>
    <div class="gui-padding bgf">
      <div class="mb15">
        <a-breadcrumb>
          <a-breadcrumb-item>分类管理</a-breadcrumb-item>
          <a-breadcrumb-item>分类列表</a-breadcrumb-item>
        </a-breadcrumb>
      </div>
      <div class="mb10 flex align-items justify-content-between">
        <div>
          <span class="mr5"><a-button type="primary" size="small" :loading="addLoading" @click="handleOpenAdd"> <template #icon><PlusOutlined/> </template>添加</a-button></span>
        </div>
        <a-form layout="inline"
                autocomplete="off">
          <a-form-item label="状态">
            <a-select
                ref="select"
                size="small"
                placeholder="请选择"
                allow-clear
                @change="handleSearch"
                v-model:value="queryParams.status"
                style="width: 120px"
            >
              <a-select-option :value="1">发布</a-select-option>
              <a-select-option :value="0">未发布</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="标题">
            <a-input v-model:value="queryParams.keyword" size="small" @change="handleSearch" style="width: 160px" placeholder="请输入标题" allow-clear/>
          </a-form-item>
          <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
            <a-button type="primary" @click.prevent="handleSearch" size="small">
              <SearchOutlined/>
              搜索
            </a-button>
          </a-form-item>
        </a-form>
      </div>
      <a-table :columns="columns"
               rowClassName="items"
               :loading="false"
               :scroll="{ x: 'calc(100vw - 326px)', y: 'calc(100vh - 268px)' }"
               :pagination="false"
               :data-source="tableData"
               size="middle" sticky bordered>
        <template #bodyCell="{ column, text, record }">
          <template v-if="column.dataIndex === 'status'">
            <a-switch v-model:checked="record.status" @change="handleChangeUpdate(record,'status')" :checked-value="1" :un-checked-value="0" checked-children="上架中"
                      un-checked-children="下架中"/>
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <div class="editable-row-operations">
            <span class="mr5">
             <a-button type="primary" size="small" @click="handleEdit(record.id)"> <template #icon><EditOutlined style="font-size:12px"/></template>编辑</a-button>
            </span>
              <span>
              <a-popconfirm title="你确定删除吗?" @confirm="handleDelete(record.id)">
               <a-button type="primary" size="small" danger> <template #icon><CloseOutlined
                   style="font-size:12px"/></template>删除</a-button>
              </a-popconfirm>
            </span>
            </div>
          </template>
        </template>
      </a-table>
      <div class="flex  mt20">
        <a-pagination
            v-model:current="queryParams.pageNo"
            v-model:page-size="queryParams.pageSize"
            @change="handleChangePageNo"
            :page-size-options="pageSizeOptions"
            show-size-changer
            :total="tableData.length"/>
      </div>
      <shops-add-category ref="addRef" @success="handleLoadData"></shops-add-category>
    </div>
  </div>
</template>
<script lang="ts" setup>
const tableData = ref([]);
const total = ref(0)
const pages = ref(0)
const addLoading = ref(false)
const addRef = ref({})
const pageSizeOptions = ref<string[]>(['10', '20', '30', '40', '50']);
const queryParams = reactive({
  "pageNo": 1,
  "pageSize": 20,
  "status": null,
  "keyword": null
})

// 获取选择的行id
const state = reactive({
  selectedRowKeys: [],
  loading: false,
})

// 定义表格的列信息
const columns = [
  {
    title: '序号',
    fixed: "left",
    align:"center",
    width: 60,
    dataIndex: 'index',
    customRender: ({text, record, index}) =>  index + 1
  },
  {
    title: '分类编号',
    align:"center",
    dataIndex: 'id',
    width: '80px',
  },
  {
    title: '分类名称',
    dataIndex: 'name',
    align:"center",
    width: '300px',
  },
  {
    title: '分类排序',
    dataIndex: 'sorted',
    align:"center",
    width: '100px',
  },
  {
    title: '是否发布',
    align: 'center',
    dataIndex: 'status',
    width: '120px',
  },
  {
    title: '操作',
    dataIndex: 'operation',
    align: "center",
    fixed: "right",
    width: '200px',
  },
];

// 查询表格信息
const handleLoadData = async ()=>{
  const res= await useLoadShopCategoryList(queryParams)
  tableData.value = res.data.records
  total.value = res.data.total
  pages.value = res.data.pages
}

// 搜索
const handleSearch = ()=>{
  queryParams.pageNo = 1;
  handleLoadData()
}

// 改变分页
const handleChangePageNo = (pno, psize) => {
  queryParams.pageNo = pno;
  queryParams.pageSize = psize;
  handleLoadData()
}


const handleOpenAdd = ()=>{
  addLoading.value = true
  addRef.value.handleOpen(null,tableData.value.length || 0)
  addLoading.value = false
}

const handleEdit = (id) => {
  addLoading.value = true
  addRef.value.handleOpen(id,tableData.value.length || 0)
  addLoading.value = false
}

const handleDelete = async(id) => {
  const res= await useDeleteShops(id)
  useMessage().success("删除成功!");
  handleLoadData()
}

const handleChangeUpdate = async(record,field)=>{
  var params = {
    id:record.id,
    field:field,
    value:record[field]
  }
  await useUpdateStatusShopsCategory(params)
  useMessage().success("操作成功!")
}

onMounted(()=>{
  handleLoadData()
})
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
